#include "stdafx.h"

#ifndef DLL_TOKENIZER_CPP
#define DLL_TOKENIZER_CPP

#include "Tokenizer.h"

inline TList* Tokenize(const char* mystring, const char* delimiters, const char* symbols)
{
	TList*list = new TList();
	
	int lenA=strlen(mystring);if (lenA<1) return list;
	int lenB=strlen(delimiters);
	int lenC=strlen(symbols);
	bool UseDelim=(lenB>0);
	bool UseSymbol=(lenC>0);
	if (!UseDelim && !UseSymbol) return list;

	int a=0,b=0,c=0, a1=0;
	char ca=0,cb=0;
	
	for (a=0;a<lenA;a++)
	{
		ca=mystring[a];

		bool found=false;
		
		if (UseDelim)
		{
			for (b=0;b<lenB;b++)
			{
				if (delimiters[b]==ca)
				{
					if ((a-a1)>0)
					{
						char* dest = (char*)malloc((a-a1+1)*sizeof(char));
						int i=0;
						for (i=a1;i<a;i++){dest[i-a1]=mystring[i];};
						dest[a-a1]='\0';
						ListAddLast(list,dest);
					};
					a1=a+1;found=true;break;
				};
			};
		};
		
		if (UseSymbol && found==false)
		{
			for (c=0;c<lenC;c++)
			{
				if (symbols[c]==ca)
				{
					if ((a-a1)>0)
					{
						char* dest = (char*)malloc((a-a1+1)*sizeof(char));
						int i=0;
						for (i=a1;i<a;i++){dest[i-a1]=mystring[i];};
						dest[a-a1]='\0';
						ListAddLast(list,dest);
					};
					
					char * dest = (char*)malloc((2)*sizeof(char));
					dest [0] = symbols[c];
					dest [1] = '\0';
					ListAddLast(list,dest);
					a1=a+1;found=true;break;
				};
			};
		};
		
		if (a==lenA-1 && found==false)
		{
			char* dest = (char*)malloc((a-a1+2)*sizeof(char));
			int i=0;
			for (i=a1;i<a+1;i++){dest[i-a1]=mystring[i];};
			dest[a-a1+1]='\0';
			ListAddLast(list,dest);
		};
	};
	return list;
}

BBAEXP int BBACALL cTokenize(const char* mystring, const char* delimiters, const char* symbols)
{
	return TobbObject(Tokenize(mystring, delimiters, symbols));
}


#endif //DLL_TOKENIZER_CPP